import { baseInfo } from '@/api/login.js';
import encryption from '@/libs/encryption.js';
import axios from '../../libs/axios';
import {getFileMax} from '@/api/fileMax'
export default {
    namespaced: true,
    state: {
        userId: '',
        userName: '',
        username: '',
        nickname: '',
        companyName: '',
        companyId: '',
        avatar: '',
        isRootDesigner: '',
        isReferee: '',
        NewNameList: [],
        distributedRange: false, //分布式主靶场
    },
    getters: {},
    mutations: {
        setUserInfo(state, info) {
            Object.keys(info).forEach((key) => {
                state[key] = info[key];
            });
        },
        setNewNameList(state, list) {
            state.NewNameList = list;
        },
        serDistributedRange(state, val) {
            state.distributedRange = val;
        }
    },
    actions: {
        async getUserInfo({ commit }) {
            let initStateUserInfo = {
                userId: '',
                userName: '',
                nickname: '',
                companyName: '',
                companyId: '',
                avatar: '',
                isRootDesigner: '',
                isReferee: ''
            };
            let returnData = await baseInfo().catch((err) => {
                console.log(err);
                commit('setUserInfo', initStateUserInfo);
            });
            if(!returnData){
                commit('setUserInfo',initStateUserInfo)
            }
            let data = returnData ? returnData.data : {};

            let userInfo = data.data
            let roleMap = {
                '单位管理员':'leader', //单位管理员
                '单位设计师':'member', // 单位设计师
                '裁决评估人员':'evaluator', // 评估
                '裁判':'referee' // 裁判
            }
            userInfo.baseRole = userInfo.role
            userInfo.role = roleMap[userInfo.roleName]
            
    
            
            
            let { code,  message } = data;//data: userInfo,
            if (code == 1) {
                let {
                    id,
                    avatar,
                    distributed,
                    role,
                    nickname,
                    companyName,
                    companyId,
                    username,
                    rangeId
                } = userInfo;
                avatar = avatar ? avatar : require('@/assets/baseInfo/defult.jpg');
                userInfo.avatar = avatar;
                userInfo.userId = id;
                userInfo.userName = username;
                // 单位管理员或者单位设计师
                let isRootDesigner, roleTxt, isReferee;
                if (role && role === 'leader') {
                    isRootDesigner = true;
                } else {
                    isRootDesigner = false;
                }
                if (role === 'leader') {
                    roleTxt = '单位管理员';
                    isReferee = false;
                } else if (role === 'member') {
                    roleTxt = '单位设计师';
                    isReferee = false;
                } else if (role === 'evaluator') {
                    roleTxt = '评估人员';
                    isReferee = false;
                } else if (role === 'referee') {
                    roleTxt = '裁判人员';
                    isReferee = true;
                }
                userInfo.roleTxt = roleTxt;
                userInfo.isRootDesigner = isRootDesigner;
                userInfo.isReferee = isReferee;
                let November = {
                    userId: id,
                    userName: username,
                    nickname,
                    companyName,
                    companyId,
                    avatar,
                    isRootDesigner,
                    isReferee,
                    distributed,
                    rangeId
                };
                sessionStorage.setItem('November', encryption.SelfUseEncrypt(JSON.stringify(November)));
                commit('setUserInfo', userInfo);
            } else {
                commit('setUserInfo', initStateUserInfo);
            }
            return { code, data: userInfo, message };
        }
    }
};
